//
//  LYOrzWebViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/18.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYOrzWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "LYTeacherWebViewController.h"
#import "LYOrderDetailViewController.h"
#import "LYOrderDetailModel.h"
#import "VoipCallController.h"
#import "LYChatFriendsModel.h"
#import "ChatViewController.h"
#import "VideoViewController.h"
#import "LYCommitOrderViewController.h"
#import <UMSocial.h>

#define orzMainUrl @"http://esj.zzzedu.cn/webview/view/org/org.php?"

@interface LYOrzWebViewController ()<UMSocialUIDelegate, UIWebViewDelegate>


@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (copy, nonatomic) NSString *urlStr;

@property (nonatomic, copy) NSString *ID;
@property WebViewJavascriptBridge *bridge;

@end

@implementation LYOrzWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.fd_prefersNavigationBarHidden = YES;
    
    [self setUpWebView];
    [self buildBridge];
    
}


#pragma mark - 构造方法
+ (instancetype)orzWebVCWithID:(NSString *) ID{
    
    LYOrzWebViewController *webVC = [[self alloc] init];
    webVC.ID = ID;
    return webVC;
}

#pragma mark - 重写方法
- (void)setID:(NSString *)ID{
    
    _ID = [ID copy];
    
    NSString *parameter = [NSString stringWithFormat:@"to_user_id=%@&token=%@",self.ID,LYToken];
    self.urlStr = [orzMainUrl stringByAppendingString:parameter];
    
}

#pragma mark - 初始化
/**
 *  初始化webView
 */
- (void)setUpWebView{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //自适应屏幕
    self.webView.scalesPageToFit = YES;
    
    //加载请求
    if(self.urlStr){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
        
        [self.webView loadRequest:request];
    }
}

/**
 *  桥接JS
 */
- (void)buildBridge{
    
    if (_bridge) { return; }
    
    __weak typeof(self) weakSelf = self;
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        
        
        
    }];
    
    
    //返回
    [_bridge registerHandler:@"goback" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    }];
    
    
    //教师页
    [_bridge registerHandler:@"gotoTeacOrOrgzHome" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        LYTeacherWebViewController *teacherVC = [LYTeacherWebViewController teaWebVCWithID:data[@"user_id"]];
        [weakSelf.navigationController pushViewController:teacherVC animated:YES];
        
    }];
    
    //支付桥接
    [_bridge registerHandler:@"buyRightNow" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
            
            [weakSelf addOrderWithID:data[@"course_id"]];
        }];
        
    }];
    



}

#pragma mark - 网络请求
/**
 *  关注
 */
- (void)follow{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool followWithToken:LYToken to_user_id:self.ID success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"关注成功"];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
    }];
    
}

/**
 *  新增订单
 */
- (void)addOrderWithID:(NSString *)ID{
    
    LYCommitOrderViewController *commitVC = [[LYCommitOrderViewController alloc] init];
    commitVC.course_id = self.ID;
    [self.navigationController pushViewController:commitVC animated:YES];
    
    
}


#pragma mark - 按钮点击
/**
 *  关注
 */
- (IBAction)followBtnOnclick:(id)sender {
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        [self follow];
    }];
}


/**
 *  分享
 */
- (IBAction)shareBtnOnclick:(id)sender {
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        WS(weakSelf);
        [_bridge callHandler:@"share" data:nil responseCallback:^(id responseData) {
            [weakSelf shareText:responseData[@"intro"]];
        }];
        
        
    }];
}


/**
 *  分享
 */
- (void)shareText:(NSString *)text{
    
    //如果需要分享回调，请将delegate对象设置self，并实现下面的回调方法
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:UMSocialAppKey
                                      shareText:text
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToSina]
                                       delegate:self];
}


/**
 *  咨询
 */
- (IBAction)callBtnOnclick:(id)sender {
    
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        
        
        [self consult];
        
    }];

    
}

/**
 *  咨询
 */
- (void)consult{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    WS(weakSelf);
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"在线咨询" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf.bridge callHandler:@"getInfo" data:nil responseCallback:^(id responseData) {
            LYChatFriendsModel *friend = [[LYChatFriendsModel alloc] init];
            friend.head_pic = responseData[@"head_pic"];
            friend.user_id = responseData[@"to_user_id"];
            friend.nick_name = responseData[@"nick_name"];
            friend.voip_account = responseData[@"voip_account"];
            
            ChatViewController *cvc = [[ChatViewController alloc] initWithSessionId:friend.voip_account];
            cvc.friendModel = friend;
            cvc.title = [[DemoGlobalClass sharedInstance] getOtherNameWithPhone:friend.voip_account];
            [weakSelf.navigationController pushViewController:cvc animated:YES];
            
        }];
      
        
    }];
    
    [alert addAction:action];
    
    UIAlertAction *actionBaidu = [UIAlertAction actionWithTitle:@"拨打电话" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [_bridge callHandler:@"getMobile" data:nil responseCallback:^(id responseData) {
            
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",responseData];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            
        }];
        
        
        
    }];
    
    [alert addAction:actionBaidu];
    
    
    UIAlertAction *actionGaode = [UIAlertAction actionWithTitle:@"视频聊天" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf.bridge callHandler:@"getInfo" data:nil responseCallback:^(id responseData) {
            
            NSString *nick_name = responseData[@"nick_name"];
            NSString *voipID = responseData[@"voip_account"];
            
            VideoViewController * vvc = [[VideoViewController alloc]initWithCallerName:nick_name andVoipNo:voipID andCallstatus:0];
            [weakSelf presentViewController:vvc animated:YES completion:nil];
            
        }];
        
    }];
    
    
    
    [alert addAction:actionGaode];
    
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action2];
    
    
    [self presentViewController:alert animated:YES completion:nil];

}

#pragma mark - webView代理

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showMessage:nil];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    
    [MBProgressHUD hideHUD];
    [MBProgressHUD showError:@"网络不给力" toView:webView];
}

#pragma mark - UMDelegate

-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData{


    if (platformName == UMShareToQQ) {
        
        socialData.extConfig.qqData.url = self.urlStr;
        
    }else if(platformName == UMShareToQzone){
    
        socialData.extConfig.qzoneData.url = self.urlStr;
        
    }else if (platformName == UMShareToSina){
    
        socialData.extConfig.sinaData.urlResource.url = self.urlStr;
    
    }else if (platformName == UMShareToWechatSession){
        
        socialData.extConfig.wechatSessionData.url = self.urlStr;
    
    }else if (platformName == UMShareToWechatTimeline){
    
        socialData.extConfig.wechatTimelineData.url = self.urlStr;
    
    }
}


@end
